package com.occludens.admin.ui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.googlecode.objectify.Ref;
import com.googlecode.objectify.annotation.Cache;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.annotation.Load;
import com.googlecode.objectify.annotation.Subclass;
import com.occludens.admin.datamodel.Role;
import com.occludens.admin.datamodel.User;


@Subclass(index=true)
@Cache
public class Command extends AdminObjectUI {
	@Index private String link;
	@Load private List<Ref<Object>> access = new ArrayList<Ref<Object>>();
	
	protected Command() {} // Mandatory for Objectify
	
	public Command(String name) {
		super(name);
	}
	
	public String getLink() {
		return this.link;
	}
	
	public void setLink(String link) {
		this.link = link;
	}

	public List<Object> getAccess() {
	    List<Object> returnList = new ArrayList<Object>();
	    Iterator<Ref<Object>> accessItr = this.access.iterator();
	    while (accessItr.hasNext()) {
	    	returnList.add(accessItr.next().get());
	    }//End of while accessItr
	    return returnList;
	}

	public void setAccess(List<Object> accesses) {
		Iterator<Object> accessesItr = accesses.iterator();
	    while (accessesItr.hasNext()) {
	    	Object access = accessesItr.next();
	    	if (access.getClass().equals(User.class) || access.getClass().equals(Role.class)) {
	    		Ref<Object> accessRef = Ref.create((Object)access);
	    		if (!this.access.contains(accessRef)) {
	    			this.access.add(accessRef);
	    		}
	    	}
	    }//End of while accessesItr
	}
	
	public void addAccess(Object... accesses) {
		for (int i=0; i<accesses.length; i++) {
			Object access = accesses[i];
			if (access.getClass().equals(User.class) || access.getClass().equals(Role.class)) {
	    		Ref<Object> accessRef = Ref.create((Object)access);
	    		if (!this.access.contains(accessRef)) {
	    			this.access.add(accessRef);
	    		}
	    	}
		}
	}
	
	public void removeAccess(Object... accesses) {
		for (int i=0; i<accesses.length; i++) {
			Object access = accesses[i];
			if (access.getClass().equals(User.class) || access.getClass().equals(Role.class)) {
	    		Ref<Object> accessRef = Ref.create((Object)access);
	    		if (this.access.contains(accessRef)) {
	    			this.access.remove(accessRef);
	    		}
	    	}
		}
	}
}
